package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_sj.domain.ReportSjnjsjBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjnjsjBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjNjsjBsInfoBo;
import com.ruoyi.report_sj.domain.vo.ReportSjNjsjBsInfoVo;
import com.ruoyi.report_sj.domain.ReportSjNjsjBsInfo;
import com.ruoyi.report_sj.mapper.ReportSjNjsjBsInfoMapper;
import com.ruoyi.report_sj.service.IReportSjNjsjBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆凝结时间试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjNjsjBsInfoServiceImpl implements IReportSjNjsjBsInfoService {

    private final ReportSjNjsjBsInfoMapper baseMapper;
    private final ReportSjnjsjBaDetailMapper detailMapper;

    /**
     * 查询砂浆凝结时间试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSjNjsjBsInfoVo queryById(Long excelId) {
        ReportSjNjsjBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSjnjsjBaDetail> detailList = detailMapper.selectListByExcelId(excelId);
        List<ReportSjnjsjBaDetail> returnList = new ArrayList<>();
        // 必须12条
        for (int i = 1; i <= 12; i++) {
            ReportSjnjsjBaDetail detail = new ReportSjnjsjBaDetail();
            if (detailList != null && detailList.size() >= i) {
                detail = detailList.get(i - 1);
            }
            detail.setIndex(i);
            returnList.add(detail);
        }
        vo.setDetailList(returnList);
        return vo;
    }

    /**
     * 查询砂浆凝结时间试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSjNjsjBsInfoVo> queryPageList(ReportSjNjsjBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjNjsjBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSjNjsjBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆凝结时间试验检测记录列表
     */
    @Override
    public List<ReportSjNjsjBsInfoVo> queryList(ReportSjNjsjBsInfoBo bo) {
        LambdaQueryWrapper<ReportSjNjsjBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjNjsjBsInfo> buildQueryWrapper(ReportSjNjsjBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjNjsjBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSjNjsjBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSjNjsjBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSjNjsjBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSjNjsjBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSjNjsjBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSjNjsjBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSjNjsjBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSjNjsjBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSjNjsjBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSjNjsjBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSjNjsjBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSjNjsjBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSjNjsjBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSjNjsjBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(bo.getJiassj1() != null, ReportSjNjsjBsInfo::getJiassj1, bo.getJiassj1());
        lqw.eq(bo.getJiassj2() != null, ReportSjNjsjBsInfo::getJiassj2, bo.getJiassj2());
        lqw.eq(bo.getNingjsj1() != null, ReportSjNjsjBsInfo::getNingjsj1, bo.getNingjsj1());
        lqw.eq(bo.getNingjsj2() != null, ReportSjNjsjBsInfo::getNingjsj2, bo.getNingjsj2());
        lqw.eq(bo.getNingjsjpj() != null, ReportSjNjsjBsInfo::getNingjsjpj, bo.getNingjsjpj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSjNjsjBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSjNjsjBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSjNjsjBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSjNjsjBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSjNjsjBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSjNjsjBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSjNjsjBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSjNjsjBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSjNjsjBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShajzl()), ReportSjNjsjBsInfo::getShajzl, bo.getShajzl());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghtj()), ReportSjNjsjBsInfo::getYanghtj, bo.getYanghtj());
        return lqw;
    }

    /**
     * 新增砂浆凝结时间试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportSjNjsjBsInfoBo bo) {
        ReportSjNjsjBsInfo add = BeanUtil.toBean(bo, ReportSjNjsjBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改砂浆凝结时间试验检测记录
     */
    @Override
    @Transactional
    public Boolean updateByBo(ReportSjNjsjBsInfoBo bo) {
        ReportSjNjsjBsInfo update = BeanUtil.toBean(bo, ReportSjNjsjBsInfo.class);
        validEntityBeforeSave(update);
        //更新子表
        List<ReportSjnjsjBaDetail> detailList = bo.getDetailList();
        if (detailList != null) {
            detailList.forEach(vo -> vo.setExcelId(bo.getExcelId()));
            detailMapper.deleteByExcelId(bo.getExcelId());
        }
        detailMapper.insertBatch(detailList);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjNjsjBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆凝结时间试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
